package org.ing.sisval.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;
import org.ing.sisval.be.BEComplementario;

public class DAComplementario extends DAOAbstract<BEComplementario> {

    public DAComplementario(Connection conn) {
        setConnection(conn);
    }

    @Override
    public int registrar(BEComplementario be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_ins_complementario");
            setParameterString(be.getNombreobra());
            setParameterString(be.getDescripcion());
            setParameterString(be.getUnidadmedida());
            setParameterDouble(be.getValorunitario());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEComplementario be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_upd_complementario");
            setParameterInt(be.getComplementarioid());
            setParameterString(be.getNombreobra());
            setParameterString(be.getDescripcion());
            setParameterString(be.getUnidadmedida());
            setParameterDouble(be.getValorunitario());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEComplementario be) throws Exception {
        try {
          setStoreProcedure("SISVAL.sp_del_complementario");
            setParameterInt(be.getComplementarioid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEComplementario> buscarReferencia(String ref) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_complementario");
            setParameterString(ref);
            commitc();
            while (existResult()) {                
                BEComplementario be= new BEComplementario();
                be.setComplementarioid(getDataInt(1));
                be.setNombreobra(getDataString(2));
                be.setDescripcion(getDataString(3));
                be.setUnidadmedida(getDataString(4));
                be.setValorunitario(getDataDouble(5));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEComplementario> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_complementario");
            setParameterString("");
            commitc();
            while (existResult()) {                
                BEComplementario be= new BEComplementario();
                be.setComplementarioid(getDataInt(1));
                be.setNombreobra(getDataString(2));
                be.setDescripcion(getDataString(3));
                be.setUnidadmedida(getDataString(4));
                be.setValorunitario(getDataDouble(5));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEComplementario buscar(int id) throws Exception {
        try {
            BEComplementario be= null;
            
            setStoreProcedure("SISVAL.sp_sel_complementario_id");
            setParameterInt(id);
            commitc();
            while (existResult()) {                
                be= new BEComplementario();
                be.setComplementarioid(getDataInt(1));
                be.setNombreobra(getDataString(2));
                be.setDescripcion(getDataString(3));
                be.setUnidadmedida(getDataString(4));
                be.setValorunitario(getDataDouble(5));
                
            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

}
